<% content_for :css do %>
  <link href="/css/select2.min.css" rel="stylesheet">
  <link href="/css/jcDate.css" rel="stylesheet" type="text/css"/>
<% end %>
<section class="wrapper">
  <!-- page start-->
  <div class="row">
    <div class="col-lg-12">
      <section class="panel">
        <header class="panel-heading">
          修 改
        </header>
        <div class="panel-body">
          <form class="form-horizontal adminex-form" >
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>用户</label>
              <div class="col-sm-10">
                <select id="select2_user" name="user_id" class="form-control"></select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>部门</label>
              <div class="col-sm-10">
                <select name="department_id" class="form-control">

                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>职位</label>
              <div class="col-sm-10">
                <select name="job_id" class="form-control">

                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>任职开始</label>
              <div class="col-sm-10">
                <input name="start_time" value="<%= @staff.start_time.to_s[0..10] %>" class="form-control jcDate">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>任职结束</label>
              <div class="col-sm-10">
                <input name="end_time" value="<%= @staff.end_time.to_s[0..10] %>" class="form-control jcDate">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">社团管理员</label>
              <div class="col-sm-10">
                <select name="society_admin" class="form-control">
                  <option value="2">否</option>
                  <option value="1" <%= @staff.society_admin == 1 ? 'selected' : '' %>>是</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">状态</label>
              <div class="col-sm-10">
                <select name="status" class="form-control">
                  <option value="1">可用</option>
                  <option value="2" <%= @staff.status == 2 ? 'selected' : '' %>>停用</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"></label>
              <div class="col-sm-10">
                <button type="button" class="btn btn-primary" id="submit_data" data-url="/admin/staffs/<%= @staff.id %>">提交</button>
              </div>
            </div>
          </form>
        </div>
      </section>
    </div>
  </div>
  <!-- page end-->
</section>
<% content_for :js do %>
  <script src="/js/select2.min.js"></script>
  <script src="/js/jQuery-jcDate.js"></script>
  <script type="text/javascript" charset="utf-8">
      $(function (){
          $(".jcDate").jcDate({
              IcoClass : "jcDateIco",
              Event : "click",
              Speed : 100,
              Left : 0,
              Top : 28,
              format : "-",
              Timeout : 100
          });
      });
      function userSelect(data) {
          data.unshift('');
          $("#select2_user").select2({
              data: data
          });//启动select2
      }
      var job_html = {};
      function renderDepartmentWithJob(data) {
          var department_html = getRenderHtml(data['department_data'],'<option value="{id}">{name}</option>');
          department_html = '<option value="0">请选择部门</option>' + department_html;
          $('select[name=department_id]').html(department_html);
          $.each(data['jobs_data'], function (i, item) {
              job_html[item[0]['department_id']] = '<option value="0">请选择职位</option>' + getRenderHtml(item,'<option value="{id}">{name}</option>');
          });
          var select2_user = $("#select2_user").select2();//获取selectid
          select2_user.val("<%= @staff.user_id %>").trigger("change");
          <% unless @staff.department_id.blank? %>
          $('select[name=department_id]').val("<%= @staff.department_id %>");
          $('select[name=job_id]').html(job_html[<%= @staff.department_id %>] || '');
          $('select[name=job_id]').val("<%= @staff.job_id %>");
          <% end %>
      }
      $('select[name=department_id]').change(function () {
          var department_id = $(this).val();
          $('select[name=job_id]').html(job_html[department_id] || '');
      });
      $('#submit_data').click(function () {
          var _this             = $(this);
          var url               = _this.attr('data-url');
          var data              = {};
          data['user_id']       = $('select[name=user_id]').val();
          data['department_id'] = $('select[name=department_id]').val();
          data['job_id']        = $('select[name=job_id]').val();
          data['start_time']    = $('input[name=start_time]').val();
          data['end_time']      = $('input[name=end_time]').val();
          data['society_admin'] = $('select[name=society_admin]').val();
          data['status']        = $('select[name=status]').val();
          doRequest(_this,url,'PUT',data);
      });
      doRequest('','/admin/staffs/get_users','GET',{},userSelect);
      doRequest('','/admin/departments/get_department_with_jobs','GET',{},renderDepartmentWithJob);
  </script>
<% end %>